<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="设备名称" prop="deviceName">
        <el-input
          v-model="queryParams.deviceName"
          placeholder="请输入设备名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="操作" prop="logOperate">
        <el-input
          v-model="queryParams.logOperate"
          placeholder="请输入操作"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['deviceLog:deviceLog:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="deviceLogList" @selection-change="handleSelectionChange" v-if="!isMobile">
      <el-table-column label="时间" align="center" prop="createTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="设备名称" align="center" prop="deviceName" />
      <el-table-column label="所属公寓/房间名称" align="center" width="130">
        <template slot-scope="scope">
          <span>{{ scope.row.hotelName }}<br>【{{ scope.row.roomName }}】</span>
        </template>
      </el-table-column>
      <el-table-column label="类型" align="center" prop="logType" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.device_log_type" :value="scope.row.logType"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" prop="logOperate" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.device_log_operate" :value="scope.row.logOperate"/>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" width="80">
        <template slot-scope="scope">
          <span style="color: rgb(30 142 62)" v-if="scope.row.status === '200'">{{ scope.row.status }}</span>
          <span style="color: rgb(241 85 50)" v-if="scope.row.status !== '200'">{{ scope.row.status }}</span>
        </template>
      </el-table-column>
      <el-table-column label="异常信息" align="center" prop="message" />
    </el-table>

    <el-table v-loading="loading" :data="deviceLogList" v-if="isMobile">
      <el-table-column label="设备日志" align="left">
        <template slot-scope="scope">
          <span style="font-size: 12px">
            <b>时间：</b>{{ parseTime(scope.row.createTime) }}<br>
            <b>设备名称：</b>{{ scope.row.deviceName }}<br>
            <b>所属公寓：</b>{{ scope.row.hotelName }}<br>
            <b>房间名称：</b>{{ scope.row.roomName }}<br>
            <b>类型：</b><dict-tag :options="dict.type.device_log_type" :value="scope.row.logType" />
            <b>操作：</b><dict-tag :options="dict.type.device_log_operate" :value="scope.row.logOperate" />
            <b>状态：</b>
              <span style="color: rgb(30 142 62)" v-if="scope.row.status === '200'">{{ scope.row.status }}</span>
              <span style="color: rgb(241 85 50)" v-if="scope.row.status !== '200'">{{ scope.row.status }}</span><br>
            <b>异常信息：</b>{{ scope.row.message }}
          </span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改设备日志对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="设备名称" prop="deviceName">
          <el-input v-model="form.deviceName" placeholder="请输入设备名称" />
        </el-form-item>
        <el-form-item label="操作" prop="logOperate">
          <el-input v-model="form.logOperate" placeholder="请输入操作" />
        </el-form-item>
        <el-form-item label="异常信息" prop="message">
          <el-input v-model="form.message" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDeviceLog, getDeviceLog, delDeviceLog, addDeviceLog, updateDeviceLog } from "@/api/device/deviceLog";
import {getJob} from "@/api/monitor/job";
import {isNumberStr} from "@/utils";
import {hasMobile} from "@/utils/ruoyi";

export default {
  name: "DeviceLog",
  dicts: ['device_log_type','device_log_operate'],
  data() {
    return {
      isMobile: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备日志表格数据
      deviceLogList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deviceName: this.$route.query.deviceName ? this.$route.query.deviceName : null,
        logType: null,
        logOperate: null,
        status: null,
        message: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        logType: [
          { required: true, message: "类型不能为空", trigger: "change" }
        ],
        logOperate: [
          { required: true, message: "操作不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created(){
    const deviceName = this.$route.query.deviceName;
    if (deviceName !== undefined && deviceName !== '') {
      this.queryParams.deviceName = deviceName;
    }
    this.getList();
    if (hasMobile()) {
      this.isMobile = true;
      this.showSearch = false;
    }
  },
  activated(){
    const deviceName = this.$route.query.deviceName;
    if (deviceName !== undefined && deviceName !== '') {
      this.queryParams.deviceName = deviceName;
    }
    this.getList();
    if (hasMobile()) {
      this.isMobile = true;
      this.showSearch = false;
    }
  },
  methods: {
    /** 查询设备日志列表 */
    getList() {
      this.loading = true;
      listDeviceLog(this.queryParams).then(response => {
        this.deviceLogList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        deviceLogId: null,
        deviceName: null,
        logType: null,
        logOperate: null,
        status: "0",
        message: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.deviceLogId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加设备日志";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const deviceLogId = row.deviceLogId || this.ids
      getDeviceLog(deviceLogId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改设备日志";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.deviceLogId != null) {
            updateDeviceLog(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDeviceLog(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const deviceLogIds = row.deviceLogId || this.ids;
      this.$modal.confirm('是否确认删除设备日志编号为"' + deviceLogIds + '"的数据项？').then(function() {
        return delDeviceLog(deviceLogIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('deviceLog/deviceLog/export', {
        ...this.queryParams
      }, `deviceLog_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
